<template>
  <el-container class="box">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :style="{width:isCollapse?'65px':'201px'}">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse-transition="false"
          :collapse="isCollapse"
          style="width:201px"
          router
          :default-active="active"
        >
          <!-- 折叠按钮 -->
          <div class="tag" @click="tabBtn" :style="{width:isCollapse?'65px':'201px'}">|||</div>
          <!-- 一级菜单 -->
          <el-submenu :index="item.path+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[item.id]"></i>
              <!-- 文本区 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+item1.path"
              v-for="item1 in item.children"
              :key="item1.id"
              @click="activeToggle('/'+item1.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本区 -->
                <span>{{item1.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //高亮
      active: "",
      //菜单折叠
      isCollapse: false,
      isTag: true,
      //请求菜单列表数据
      menuList: [],
      //小图标数据
      iconList: {
        125: "iconfont icon-users",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
    };
  },
  created() {
    this.getMenuList();
    this.activeToggle(window.sessionStorage.getItem("activePath"));
  },
  methods: {
    loginOut() {
      layer.confirm(
        "确定退出登录，重新登录吗？",
        { icon: 3, title: "提示" },
        (index) => {
          layer.msg("正在退出...", { time: 1500 }, () => {
            //清除本地token
            window.sessionStorage.clear();
            //编程式导航跳转到登录页
            this.$router.push("/login");
            layer.close(index);
          });
        }
      );
    },
    // 获取菜单列表函数
    async getMenuList() {
      // 发起请求获取菜单列表  结构data重名为res
      const { data: res } = await this.axios.get("menus");
      if (res.meta.status !== 200) {
        return layer.msg(res.meta.msg, { time: 1000 });
      }
      //将数据保存到实例data
      this.menuList = res.data;
      //获取成功渲染列表
    },
    //折叠菜单按钮
    tabBtn() {
      this.isCollapse = !this.isCollapse;
      this.isTag = !this.isTag;
    },
    // 点击当前高亮
    activeToggle(path) {
      window.sessionStorage.setItem("activePath", path);
      this.active = path;
    },
  },
};
</script>

<style scoped>
.box {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 5px !important;
  padding-right: 10px !important;
  color: #ffffff;
  font-size: 24px;
}
.el-header span {
  margin-left: 10px;
}
.el-aside {
  background: #333744;
}
.el-main {
  background-color: #e9edf1;
}
.iconfont {
  margin-right: 10px;
}
.tag {
  height: 25px;
  color: #ffffff;
  text-align: center;
  line-height: 22px;
  background-color: #4a5064;
  cursor: pointer;
  font-size: 10px;
  letter-spacing: 0.2em;
}
</style>